﻿
using Coldairarrow.Entity.MEGA;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;


namespace Coldairarrow.Business.MEGA
{
    public class MEGA_FactoryManagementBusiness : BaseBusiness<MEGA_FactoryManagement>, IMEGA_FactoryManagementBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public MEGA_FactoryManagementBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<MEGA_FactoryManagement>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<MEGA_FactoryManagement>();
            var search = input.Search;
            if (_operator.IsAdmin())
            {
                //超级管理员
            }
            else
            {
                string xzqhid = _operator.XZQHId;
                //获取该行政区划下的直属下级集合
                var Ids = GetChildrenIds(xzqhid);
                q = q.Where(x => Ids.Contains(x.AreaId));
            }
            //筛选
            //if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            //{
            //    var newWhere = DynamicExpressionParser.ParseLambda<MEGA_FactoryManagement, bool>(
            //        ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
            //    where = where.And(newWhere);
            //}
            if (!search.Keyword.IsNullOrEmpty())
            {
              q = q.Where(e => e.Name.Contains(search.Keyword) || e.Address.Contains(search.Keyword) || e.Code.Contains(search.Keyword));
            }
            q = q.OrderBy(input.SortField, input.SortType).Where(where);

            return await q.Where(where).GetPageResultAsync(input);
        }

        private List<string> GetChildrenIds(string PId)
        {

            var allNode = Db.GetIQueryable<MEGA_XZQH>().Select(x => new TreeModel
            {
                Id = x.Id,
                ParentId = x.ParentId,
                Text = x.Name,
                Value = x.Id
            }).ToList();

            var children = TreeHelper
                .GetChildren(allNode, allNode.Where(x => x.Id == PId).FirstOrDefault(), true)
                .Select(x => x.Id)
                .ToList();

            return children;
        }

        public async Task<MEGA_FactoryManagement> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(MEGA_FactoryManagement data)
        {           
            //string code = DateTime.Now.ToJsTimestamp().ToString().Substring(8, 3);
            ////生成随机3位数字
            //data.Code = "GC" + code;
            var q = Db.GetIQueryable<MEGA_FactoryManagement>().Where(x => x.Name.Equals(data.Name)).ToList();
            if (q.Count() > 0)
            {
                throw new Exception("该工厂已经存在!");
            }
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(MEGA_FactoryManagement data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}